package cn.tang.crm.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class UserServlet extends BaseServlet {
	private static final Logger logger = Logger.getLogger(UserServlet.class);
	private static final long serialVersionUID = 1L;
	
	
	public void logout(HttpServletRequest req, HttpServletResponse resp) {
		req.getSession().removeAttribute("username");
		req.getSession().removeAttribute("role");
		responseJson(resp, "1");
	}

	public void addClient(HttpServletRequest req, HttpServletResponse resp) {
		String clientName = req.getParameter("clientName");
		String username = (String) req.getSession().getAttribute("username");
		String clientType = req.getParameter("clientType");
		String region = req.getParameter("region");
		String mobilePhone1 = req.getParameter("mobilePhone1");
		String mobilePhone2 = req.getParameter("mobilePhone2");
		String telephone = req.getParameter("telephone");
		String email = req.getParameter("email");
		String profile = req.getParameter("profile");
		String ret = userService.addClient(clientName, username, clientType, region, mobilePhone1, mobilePhone2, telephone, email, profile);
		if (logger.isDebugEnabled())
			logger.debug("addClient() return :" + ret);
		responseJson(resp, ret);
	}

	/**
	 * 
	 * @param req
	 * @param resp
	 *            create table clientOrder(id varchar(36),username
	 *            varchar(100),clientName varchar(100),orderTime int,nameOfGoods
	 *            varchar(200),specifications varchar(200),unitPrice
	 *            float,salesNumber float,money float);
	 */
	public void addOrder(HttpServletRequest req, HttpServletResponse resp) {
		String username = (String) req.getSession().getAttribute("username");
		String clientName = req.getParameter("clientName");
		String orderTime = req.getParameter("orderTime");
		String nameOfGoods = req.getParameter("nameOfGoods");
		String specifications = req.getParameter("specifications");
		String unitPrice = req.getParameter("unitPrice");
		String salesNumber = req.getParameter("salesNumber");
		String money = req.getParameter("money");
		String ret = userService.addOrder(username, clientName, orderTime, nameOfGoods, specifications, unitPrice, salesNumber, money);
		if (logger.isDebugEnabled())
			logger.debug("addClient() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllClient(HttpServletRequest req, HttpServletResponse resp) {
		String username = (String) req.getSession().getAttribute("username");
		String ret = userService.listAllClient(username);
		if (logger.isDebugEnabled())
			logger.debug("listAllClient() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllOrder(HttpServletRequest req, HttpServletResponse resp) {
		String username = (String) req.getSession().getAttribute("username");
		String ret = userService.listAllOrder(username);
		if (logger.isDebugEnabled())
			logger.debug("listAllOrder() return :" + ret);
		responseJson(resp, ret);
	}

	public void deleteClient(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String ret = userService.deleteClient(id);
		if (logger.isDebugEnabled())
			logger.debug("deleteClient() return :" + ret);
		responseJson(resp, ret);
	}

	public void deleteOrder(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String ret = userService.deleteOrder(id);
		if (logger.isDebugEnabled())
			logger.debug("deleteOrder() return :" + ret);
		responseJson(resp, ret);
	}

	public void getClientNameOption(HttpServletRequest req, HttpServletResponse resp) {
		String username = (String) req.getSession().getAttribute("username");
		String ret = userService.getClientNameOption(username);
		if (logger.isDebugEnabled())
			logger.debug("deleteClient() return :" + ret);
		responseJson(resp, ret);
	}

}
